<script setup>
import { ElMessage } from 'element-plus';
import Layer from "@/components/layer/index.vue";
import { defineProps, ref, defineEmits } from 'vue'
import { addAPI, updateAPI } from '@/api/wedding/rightInfo'

// 使用defineEmits来接收来自父组件的方法
const emit = defineEmits(['getTableData'])
// 声明props
const props = defineProps({
  layer: {
    type: Object,
    default: () => {
      return {
        show: false,
        title: '',
        showButton: true
      }
    }
  }
});

/************************************定义变量************************************************/
const ruleForm = ref(null)
const layerDom = ref(null)
const rules = {
  // isPrivacy: [{ required: true, message: '请输入是否隐私政策推送', trigger: 'blur' }],
  // privacy: [{ required: true, message: '请输入隐私政策内容', trigger: 'blur' }],
  // isAgreement: [{ required: true, message: '请输入是否用户协议推送', trigger: 'blur' }],
  // agreement: [{ required: true, message: '请输入用户协议内容', trigger: 'blur' }],
  // isDiscount: [{ required: true, message: '请输入优惠卷开关', trigger: 'blur' }],
  // withdrawalFee: [{ required: true, message: '请输入用户提现手续费比例', trigger: 'blur' }],
  // commission: [{ required: true, message: '请输入平台抽佣比例', trigger: 'blur' }],
  // fileSize: [{ required: true, message: '请输入上传图片大小', trigger: 'blur' }],
}
let form = ref({
  id: null,
  isPrivacy: null,
  privacy: null,
  isAgreement: null,
  agreement: null,
  isDiscount: null,
  withdrawalFee: null,
  commission: null,
  creator: null,
  fileSize: null,
  createTime: null,
  updateTime: null,
  delFlag: null,
})

/************************************数据回显************************************************/
if (props.layer.row) {
  if (props.layer.row) {
    let source = props.layer.row
    Object.keys(form.value).forEach(k => k in source && (form.value[k] = source[k]));
  }
}

/************************************定义事件************************************************/
//提交按钮事件
let submit = () => {
  if (ruleForm) {
    ruleForm.value.validate((valid) => {
      if (valid) {
        let params = form.value
        if (props.layer.row) {
          updateForm(params)
        } else {
          addForm(params)
        }
      } else {
        return false;
      }
    });
  }
}

let updateForm = (params) => {
  updateAPI(params)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '编辑成功'
      })
      emit('getTableData', false)
      layerDom && layerDom.value.close()
    })
}

let addForm = (params) => {
  addAPI(params)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '新增成功'
      })
      emit('getTableData', true)
      layerDom && layerDom.value.close()
    })
}
</script>

<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right:30px;">
      <el-form-item label="是否隐私政策推送：" prop="isPrivacy">
        <el-select v-model="form.isPrivacy" placeholder="请选择是否隐私政策推送">
          <el-option label="是" value="0"></el-option>
          <el-option label="否" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="隐私政策内容：" prop="privacy">
        <el-input type="textarea" v-model="form.privacy" placeholder="请输入隐私政策内容"></el-input>
      </el-form-item>
      <el-form-item label="是否用户协议推送：" prop="isAgreement">
        <el-select v-model="form.isAgreement" placeholder="请选择是否隐私政策推送">
          <el-option label="是" value="0"></el-option>
          <el-option label="否" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户协议内容：" prop="agreement">
        <el-input type="textarea" v-model="form.agreement" placeholder="请输入用户协议内容"></el-input>
      </el-form-item>
      <el-form-item label="优惠卷开关：" prop="isDiscount">
        <el-select v-model="form.isDiscount" placeholder="请选择优惠卷开关">
          <el-option label="开" value="0"></el-option>
          <el-option label="关" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户提现手续费比例：" prop="withdrawalFee">
        <el-input v-model="form.withdrawalFee" placeholder="请输入用户提现手续费比例"></el-input>
      </el-form-item>
      <el-form-item label="平台抽佣比例：" prop="commission">
        <el-input v-model="form.commission" placeholder="请输入平台抽佣比例"></el-input>
      </el-form-item>
      <el-form-item label="上传图片大小：" prop="fileSize">
        <el-input v-model="form.fileSize" placeholder="请输入上传图片大小"></el-input>
      </el-form-item>
      <el-form-item label="客服电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入客服电话"></el-input>
      </el-form-item>
      <el-form-item label="分享文案" prop="shareContent">
        <el-input v-model="form.shareContent" placeholder="请输入分享文案"></el-input>
      </el-form-item>
      <el-form-item label="提现规则" prop="cashRule">
        <el-input v-model="form.cashRule" placeholder="请输入分享文案"></el-input>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<style scoped lang="scss">
.el-input-number--large {
  line-height: 38px;
  width: 230px;
}
</style>
